<template>
	<view class="parent-view">
		<view class="address-view" @click="goAddressList">
			<view class="address-txt-view">
				<text class="address-txt">收货地址</text>
				<image class="address-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<view class="name-view">
				<text class="address-name">{{addressName}}</text>
				<text class="address-phone">{{addressPhone}}</text>
			</view>
			<view class="address-area" v-if="addressArea">
				<image class="address-icon" src="../../static/address-icon.png"></image>{{addressArea}}
			</view>
		</view>

		<view class="goods-view">
			<text class="goods-txt">商品信息</text>
			<view class="goods-info-view">
				<image class="goods-image" :src="goodData.image"></image>
				<view class="goods-info-txt-view">
					<text class="goods-title">{{goodData.name}}</text>
					<text class="goods-desc">{{goodData.sub}}</text>
					<text class="goods-price">¥{{getNumber(goodGGPrice)}}</text>
				</view>
			</view>
			<view class="goods-price-view">
				<text class="dingdan-txt">订单金额：</text>
				<text class="dingdan-price">{{getNumber(goodGGPrice*goodsCount)}}元</text>
				<text class="goods-count-txt">数量：</text>
				<view class="goods-count-view">
					<text class="goods-count-sub" @click="goodsSub">-</text>
					<text class="goods-count">{{goodsCount}}</text>
					<text class="goods-count-add" @click="goodsAdd">+</text>
				</view>
			</view>
			<view class="beizhu-view">
				<text class="beizhu-txt">备注：</text>
				<input class="beizhu-input" placeholder="请输入备注信息" @input="beizhuInput"/>
			</view>
		</view>

		<view class="dikou-view" @click="showDiKouPop" v-if="cash>0">
			<text class="dikou-txt">余额抵扣</text>
			<text class="dikou-desc" v-if="dikouType==1">已抵扣{{getNumber(dikouPrice)}}</text>
			<text class="dikou-desc" v-else>不使用抵扣</text>
			<image class="dikou-arrow" src="../../static/right-arrow.png"></image>
		</view>

		<view class="pay-btn" @click="creatOrder" v-if="payPrice!=0">立即支付{{getNumber(payPrice)}}元</view>
		<view class="pay-btn" @click="creatOrder" v-else>立即抵扣</view>
		<goodsBuyDikouPop :isShow="isShowDikouPop" :dataCash="cash" :dataDiKou="dikouPrice" :typeDiKou="dikouType"
		 @returnGoodsBuyDiKouPop="goodsBuyDiKouPopReturn" />
	</view>
</template>

<script>
	import goodsBuyDikouPop from "@/components/goods-buy-dikou-pop/goods-buy-dikou-pop.vue";
	import request from '../index/request.js';
	import { add, reduce, ride, except } from '@/commom/decimal-utils'

	export default {
		components: {
			goodsBuyDikouPop
		},
		data() {
			return {
				addressName: "请选择收获地址",
				addressPhone: "",
				addressArea: "",
				addressId: "",
				goodData: {},
				skuId: 0,
				goodGGPrice: "",
				goodsCount: 0,
				cash: 0.00,
				payPrice: 0.00,
				dikouType: 1,
				dikouPrice: 0.00,
				isShowDikouPop: false,
				beizhuStr:"",
			}
		},
		
		methods: {
			onLoad: function(options) {
				this.appUtil.saveAppInviteId(options)
				var fromData = JSON.parse(decodeURIComponent(options.fromData))
				this.goodData = fromData.goodData
				this.goodsCount = fromData.goodsCount
				this.skuId = fromData.skuId
				this.goodGGPrice = fromData.goodGGPrice
				this.getCashData()
				this.getAddressData()
				var that=this
				uni.$on('updateAddress',function(data){
					that.adapterAddressData(data)
				})
			},
			onUnload:function(){
				uni.$off('updateAddress')
			},
			onShow: function() {
				this.share.url = '/pages/tabBar/home/index?appInviteId=' + this.appUtil.getAppInviteId()
				this.share.shareType = 1
			},
			getNumber: function(data){
				return parseFloat(data).toFixed(2);
			},
			beizhuInput:function(e){
				this.beizhuStr = e.target.value
			},
			getAddressData: function() {
				var that = this
				request.post("api/mall/GetUserAddressList", {}).then(data => {
					for (var i = 0; i < data.length; i++) {
						if (data[i].isDefault == 1) {
							that.adapterAddressData(data[i])
							break;
						}
					}
				})
			},
			adapterAddressData:function(data){
				var that = this
				that.addressName = data.name
				that.addressPhone = data.phone
				that.addressArea = data.address + data.detail
				that.addressId = data.id
			},
			getCashData: function() {
				var that = this
				request.post("api/user/user_agency_ao", {}).then(data => {
					that.cash = data.cash
					that.getPayPrice()
				})
			},
			goodsSub: function() {
				if (this.goodsCount > 1) {
					this.goodsCount = this.goodsCount - 1
					this.getPayPrice()
				}
			},
			goodsAdd: function() {
				this.goodsCount = this.goodsCount + 1
				this.getPayPrice()
			},
			getPayPrice: function() {
				if (this.dikouType == 0) {
					this.payPrice = this.goodGGPrice * this.goodsCount
				} else {
					if (this.cash > this.goodGGPrice * this.goodsCount) {
						this.payPrice = 0
						this.dikouPrice=this.goodGGPrice * this.goodsCount
					} else {
						this.payPrice = this.goodGGPrice * this.goodsCount-this.cash
						this.dikouPrice=this.cash
					}
				}
			},
			showDiKouPop: function() {
				this.isShowDikouPop = true
			},
			goodsBuyDiKouPopReturn: function(e) {
				console.log(JSON.stringify(e))
				this.isShowDikouPop = e.isclose
				if (e.tag) {
					this.dikouType = e.type
					this.getPayPrice()
				}
			},
			goAddressList:function(){
				uni.navigateTo({
					url:"../shopping-goods-address-list/index"
				})
			},
			creatOrder:function(){
				if(!this.addressId){
					uni.showToast({
						title:"请添加收货地址"
					})
					return
				}
				var updata={}
				var goodsList=[]
				var goods={}
				goods.goods_id=this.goodData.id
				goods.sku_id=this.skuId
				goods.num=this.goodsCount
				goodsList.push(goods)
				updata.list=goodsList
				updata.address_id=this.addressId
				var that = this
				request.post("api/mall/add_mall_order", {
					goods_group:JSON.stringify(updata),
					pay_type:"miniapp",
					is_cash:this.dikouType,
					remark:this.beizhuStr,
					openid:uni.getStorageSync("shiqin-user-openId")
				}).then(data => {
					if(!data.paySign){
						uni.showToast({
							title: '支付成功',
							icon: 'none',
							mask: true,
							duration: 1000
						});
						uni.navigateBack()
						//进入订单详情页面
						uni.navigateTo({
							url:"../shopping-goods-order-detail/index?ordersn="+data.ordersn
						})
					}else{
						wx.requestPayment({
							provider: 'wxpay',
							timeStamp: data.timeStamp,
							nonceStr: data.nonceStr,
							package: data.package,
							paySign: data.paySign,
							signType: data.signType,
							success: function (res) {
								uni.showToast({
									title: '支付成功',
									icon: 'none',
									mask: true,
									duration: 1000
								});
								uni.navigateBack()
								//进入订单详情页面
								uni.navigateTo({
									url:"../shopping-goods-order-detail/index?ordersn="+data.ordersn
								})
							},
							fail: function (err) {
								console.log('fail:' + JSON.stringify(err));
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									mask: true,
									duration: 1000
								});
							}
						});
					}
				})
				
			}
		}
	}
</script>

<style>
	@import "index.css";
</style>
